package com.maple.common.widget

import android.content.Context
import android.content.res.ColorStateList
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.annotation.ColorInt
import com.maple.common.R
import com.maple.common.databinding.WidgetChoicceViewBinding
import com.maple.common.ext.click
import androidx.core.content.withStyledAttributes


/**
 * Tip:
 * Create by SeVen on 2024/4/17 19:43
 */
class ChoiceView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {

    private val binding: WidgetChoicceViewBinding

    private var isEditable: Boolean = true

    init {
        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        binding = WidgetChoicceViewBinding.inflate(inflater, this, true)

        initAttrs(context, attrs)
        initView()
    }

    private fun initView() {
        binding.tvChoiceContent.click {
            if (isEditable) {
                onArrowRightClickListener?.invoke()
            }
        }
    }

    private fun initAttrs(context: Context, attrs: AttributeSet?) {
        context.withStyledAttributes(attrs, R.styleable.ChoiceView) {
            for (i in 0 until indexCount) {
                when (val attr = getIndex(i)) {
                    R.styleable.ChoiceView_choiceTitle -> {
                        val resId = getString(attr)
                        binding.tvChoiceTitle.text = resId
                    }

                    R.styleable.ChoiceView_choiceTitleColor -> {
                        val color = getColor(attr, 0)
                        binding.tvChoiceTitle.setTextColor(color)
                    }

                    R.styleable.ChoiceView_choiceTitleSize -> {
                        val size = getDimension(attr, 0f)
                        binding.tvChoiceTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, size)
                    }

                    R.styleable.ChoiceView_choiceText -> {
                        val resId = getString(attr)
                        binding.tvChoiceContent.text = resId
                    }

                    R.styleable.ChoiceView_choiceTextColor -> {
                        val color = getColor(attr, 0)
                        binding.tvChoiceContent.setTextColor(color)
                    }

                    R.styleable.ChoiceView_choiceTextSize -> {
                        val size = getDimension(attr, 0f)
                        binding.tvChoiceContent.setTextSize(TypedValue.COMPLEX_UNIT_PX, size)
                    }

                    R.styleable.ChoiceView_choiceHintText -> {
                        val resId = getString(attr)
                        binding.tvChoiceContent.hint = resId
                    }

                    R.styleable.ChoiceView_choiceHintTextColor -> {
                        val color = getColor(attr, 0)
                        binding.tvChoiceContent.setHintTextColor(color)
                    }

                    R.styleable.ChoiceView_rightIcon -> {
                        val drawable = getDrawable(attr)
                        drawable?.setBounds(
                            0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight
                        )
                        binding.tvChoiceContent.setCompoundDrawables(null, null, drawable, null)
                    }

                    R.styleable.ChoiceView_isEditable -> {
                        isEditable = getBoolean(attr, true)
                    }

                    R.styleable.ChoiceView_showDividerLine -> {
                        val show = getBoolean(attr, false)
                        val visible = if (show) VISIBLE else GONE
                        binding.dividerLine.visibility = visible
                    }

                    R.styleable.ChoiceView_android_minHeight -> {
                        val minHeight = getDimension(attr, 0f)
                        binding.root.minimumHeight = minHeight.toInt()
                    }
                }
            }
        }
    }

    /**
     * 是否可编辑
     */
    fun setChoice(enable: Boolean) {
        isEditable = enable
    }

    /**
     * 获取标题
     */
    fun getChoiceTitle(): String {
        return binding.tvChoiceTitle.text.toString()
    }

    /**
     * 设置输入内容
     */
    fun setChoiceContent(content: String) {
        binding.tvChoiceContent.text = content
    }

    fun setChoiceContentColor(@ColorInt color: Int) {
        binding.tvChoiceContent.setTextColor(ColorStateList.valueOf(color))
    }

    fun setChoiceContentColor(colors: ColorStateList?) {
        binding.tvChoiceContent.setTextColor(colors)
    }

    /**
     * 获取输入的内容
     */
    fun getChoiceContent(): String {
        return binding.tvChoiceContent.text.toString()
    }

    private var onArrowRightClickListener: (() -> Unit)? = null

    fun setOnArrowRightClickListener(onArrowRightClickListener: (() -> Unit)?) {
        this.onArrowRightClickListener = onArrowRightClickListener
    }
}